#include "stateagent.h"

#include "sm_queue.h"
#include "sm_state_machine.h"


int stateagent_create()
{
    int iResult = OK;
    if(iResult == OK)
    {
        iResult = sm_queue_create();
    }

    if(iResult == OK)
    {
        iResult = sm_state_machine_create();
    }
    if(iResult == OK)
    {

    }
    return iResult;
}

int stateagent_destory()
{
    int iResult = OK;
    if(iResult == OK)
    {
        iResult = sm_queue_destory();
    }
    if(iResult == OK)
    {
        iResult = sm_state_machine_destory();
    }
    if(iResult == OK)
    {

    }
    return iResult;
}

int stateagent_initialize()
{
    int iResult = OK;
    int state_check_result = OK;
    printf("%s > ()\n",__func__);

    if(iResult == OK)
    {
        iResult = sm_state_machine_check_transition(SM_EVENT_INITIALIZE, &state_check_result);
        if(iResult == OK && state_check_result != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_check_transition error, line=%d",__LINE__);
        }
    }
    if(iResult == OK)
    {
        iResult = sm_state_machine_perform_transition(SM_EVENT_INITIALIZE);
        if(iResult != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_perform_transition error, line=%d",__LINE__);
        }
    }
    if(iResult == OK)
    {

    }

     printf("%s < () = 0x%x\n",__func__,iResult);

    return iResult;
}

int stateagent_terminate()
{
    int iResult = OK;
    int state_check_result = OK;
    printf("%s > ()\n",__func__);

    if(iResult == OK)
    {
        iResult = sm_state_machine_check_transition(SM_EVENT_TERMINATE, &state_check_result);
        if(iResult == OK && state_check_result != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_check_transition error, line=%d",__LINE__);
        }
    }
    if(iResult == OK)
    {
        iResult = sm_state_machine_perform_transition(SM_EVENT_TERMINATE);
        if(iResult != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_perform_transition error, line=%d",__LINE__);
        }
    }
    if(iResult == OK)
    {

    }

    printf("%s < () = 0x%x\n",__func__,iResult);
    return iResult;
}

int stateagent_queue(IN int action_id, OUT SM_QUEUE_STATE_ENUM *queue_state)
{
    int iResult = OK;
    int state_check_result = OK;
    printf("%s > ()\n",__func__);

    int current_size = 0;
    int max_size = 0;

    if(NULL == queue_state)
    {
        iResult = SM4A_NULL_POINTER_ERROR;
    }

    if(iResult == OK)
    {
        iResult = sm_get_queue_size(&current_size);
    }
    if(iResult == OK)
    {
        iResult = sm_get_queue_max_size(&max_size);
    }

    SM_EVENT_ENUM event = SM_EVENT_MIN;
    if(iResult == OK)
    {
        if(current_size == max_size)
        {
            event = SM_EVENT_QUEUE_FULL;
        }else if( (current_size+1) == max_size)
        {
            event = SM_EVENT_QUEUE_FULL;
        }else if( (current_size+1) < max_size)
        {
            event = SM_EVENT_QUEUE;
        }
    }

    if(iResult == OK)
    {
        iResult = sm_state_machine_check_transition(event, &state_check_result);
        if(iResult == OK && state_check_result != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_check_transition error, line=%d\n",__LINE__);
        }
    }

    if(iResult == OK)
    {
        iResult = sm_queue(action_id, queue_state);
    }

    if(iResult == OK)
    {
        iResult = sm_state_machine_perform_transition(event);
        if(iResult != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_perform_transition error, line=%d",__LINE__);
        }
    }

    if(iResult != OK)
    {
        sm_state_machine_perform_transition(SM_EVENT_ERROR);
    }

    printf("%s < () = 0x%x\n",__func__,iResult);
    return iResult;
}

int stateagent_get_result(OUT int *action_id, OUT SM_QUEUE_STATE_ENUM *queue_state)
{
    int iResult = OK;
    int state_check_result = OK;
    printf("%s > ()\n",__func__);

    int current_size = 0;

    if(NULL == queue_state
            || NULL == action_id)
    {
        iResult = SM4A_NULL_POINTER_ERROR;
    }
    if(iResult == OK)
    {
        iResult = sm_get_queue_size(&current_size);
    }


    SM_EVENT_ENUM event = SM_EVENT_MIN;
    if(iResult == OK)
    {
        if(current_size == 1)
        {
            event = SM_EVENT_GET_RESULT_EMPTY;
        }else if( current_size > 1)
        {
            event = SM_EVENT_GET_RESULT;
        }else if( current_size < 1)
        {
            event = SM_EVENT_GET_RESULT;
        }
    }

    if(iResult == OK)
    {
        iResult = sm_state_machine_check_transition(event, &state_check_result);
        if(iResult == OK && state_check_result != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_check_transition error, line=%d\n",__LINE__);
        }
    }

    if(iResult == OK)
    {
        iResult = sm_get_result(action_id, queue_state);
    }
    if(iResult == OK)
    {
        iResult = sm_state_machine_perform_transition(event);
        if(iResult != OK)
        {
            iResult = state_check_result;
            printf("call sm_state_machine_perform_transition error, line=%d",__LINE__);
        }
    }

    if(iResult != OK)
    {
        sm_state_machine_perform_transition(SM_EVENT_ERROR);
    }

    printf("%s < () = 0x%x\n",__func__,iResult);
    return iResult;
}
